#ifndef DATERANGEDIFFCALCULATOR_H
#define DATERANGEDIFFCALCULATOR_H
#include <iostream>
#include <ctime>
#include <cmath>
#include <conio.h>

using namespace std;

class fuction6 {
public:
    void Calculate() {
        int y1, m1, d1, y2, m2, d2;
        cout << "请输入第一个日期（格式：年 月 日）：";
        cin >> y1 >> m1 >> d1;
        cout << "请输入第二个日期（格式：年 月 日）：";
        cin >> y2 >> m2 >> d2;

        if (!Date(y1, m1, d1) || !Date(y2, m2, d2)) {
            cout << "输入的日期无效，请检查年份、月份和天数是否正确。" << endl;
            cout << "按任意键返回菜单..." << endl;
            _getch();
            return;
        }

        // 转换为 time_t时间戳
        time_t t1 = timebian(y1, m1, d1);
        time_t t2 = timebian(y2, m2, d2);

        // 计算差异（单位：天）
        const int x = 60 * 60 * 24;
        int diff_days = abs(int((t2 - t1) / x));

        cout << "两个日期之间相差：" << diff_days << " 天。" << endl;
        cout << "按任意键返回菜单..." << endl;
        _getch();
    }

private:
    bool Date(int year, int month, int day) {
        if (year < 1900 || year > 2100) return false;
        if (month < 1 || month > 12) return false;
        if (day < 1 || day > 31) return false;
        static const int days[] = {
            31, 28, 31, 30, 31, 30, 31,
            31, 30, 31, 30, 31
        };

        int max_day = days[month - 1];

        // 闰年2月校验
        if (month == 2 && LeapYear(year)) {
            max_day = 29;
        }

        return day <= max_day;
    }

    bool LeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    time_t timebian(int year, int month, int day) {
        tm date_tm = {};
        date_tm.tm_year = year - 1900;
        date_tm.tm_mon = month - 1;
        date_tm.tm_mday = day;
        date_tm.tm_hour = 0;
        date_tm.tm_min = 0;
        date_tm.tm_sec = 0;
        return mktime(&date_tm);
    }
};

#endif
